﻿using SuperiorCommon;
using SuperiorModel;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace H5.SuperiorShop.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TempData["LayoutUserName"] = UserName;
            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        /// 解密参数验证。
        /// </summary>
        /// <param name="secret"></param>
        /// <returns></returns>
        protected virtual string ValidataParms(string secret)
        {
            var res = 0;
            if (int.TryParse(SecretClass.DecryptQueryString(secret), out res))
            {
                return res.ToString();
            }
            return null;
        }

        /// <summary>
        /// 返回请求成功消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="res"></param>
        /// <returns></returns>
        protected virtual JsonResult Success(object res)
        {
            var requestType = HttpContext.Request.RequestType;
            if (requestType == "GET")
                return Json(new ResultModel(true, string.Empty, res), JsonRequestBehavior.AllowGet);
            return Json(new ResultModel(true, string.Empty, res));
        }
        /// <summary>
        /// 返回请求失败的消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected virtual JsonResult Error(string msg)
        {
            var requestType = HttpContext.Request.RequestType;
            if (requestType == "GET")
                return Json(new ResultModel(false, msg), JsonRequestBehavior.AllowGet);
            return Json(new ResultModel(false, msg));
        }

        protected virtual string ErrorMsg()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //获取所有错误的Key
            List<string> Keys = ModelState.Keys.ToList();
            //获取每一个key对应的ModelStateDictionary
            foreach (var key in Keys)
            {
                var errors = ModelState[key].Errors.ToList();
                //将错误描述添加到sb中
                foreach (var error in errors)
                {
                    sb.Append(error.ErrorMessage + "| ");
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获取spid和said集合
        /// </summary>
        /// <returns></returns>
        protected virtual PublicParmsModel GetParms()
        {
            var res = new PublicParmsModel();
            int spid = 0;int said = 0;
            if (int.TryParse(SecretClass.DecryptQueryString(Request["spid"]), out spid) && int.TryParse(SecretClass.DecryptQueryString(Request["said"]), out said))
            {
                res.Spid = spid;
                res.Said = said;
            }
            return res;
        }
        /// <summary>
        /// 检测URL得SPID，并且解密
        /// </summary>
        /// <returns></returns>
        protected virtual int CheckSpid()
        {

            int spid = 0;
            if (!int.TryParse(SecretClass.DecryptQueryString(Request["spid"]), out spid))
            {
            }
            return spid;
        }
        /// <summary>
        /// 检测URL得SAID，并且解密
        /// </summary>
        /// <returns></returns>
        protected virtual int CheckSaid()
        {

            int said = 0;
            if (!int.TryParse(SecretClass.DecryptQueryString(Request["said"]), out said))
            {
            }
            return said;
        }
        /// <summary>
        /// 校验
        /// </summary>
        /// <param name="spid"></param>
        /// <returns></returns>
        protected virtual Api_WxUserLoginResutModel GetUser(int spid)
        {
            return HttpContext.Session[SessionKey.H5UserKey] as Api_WxUserLoginResutModel;
        }
    }
    public class PublicParmsModel
    {
        public int Spid { get; set; }
        public int Said { get; set; }

    }

    public class ApiResponseModel<T>
    {
        public int Code { get; set; }
        public string Message { get; set; }
        public T Data { get; set; }

    }
}